package com.study.talk.adapter;

import android.content.Intent;
import android.os.Bundle;

import android.view.ViewGroup;


import androidx.annotation.NonNull;
import androidx.core.app.ActivityOptionsCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.study.talk.app.base.BaseActivity;
import com.study.talk.app.other.ImagePreviewActivity;
import com.study.talk.databinding.MomentGridViewItemBinding;
import com.study.talk.net.OkHttp;
import com.study.talk.util.GsonInstance;

import java.util.ArrayList;

public class MomentListGridViewAdapter extends RecyclerView.Adapter<MomentListGridViewAdapter.ViewHolder> {

    ArrayList<String> imageUriList;
    BaseActivity activity;


    public static class ViewHolder extends RecyclerView.ViewHolder{

        MomentGridViewItemBinding viewItemBinding;

        public ViewHolder(@NonNull MomentGridViewItemBinding viewItemBinding) {
            super(viewItemBinding.getRoot());
            this.viewItemBinding = viewItemBinding;
        }
    }

    public MomentListGridViewAdapter(ArrayList<String> imageUriList, BaseActivity activity) {
        this.imageUriList = imageUriList;
        this.activity = activity;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(MomentGridViewItemBinding.inflate(activity.getLayoutInflater(),parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 设置图片网格的点击事件
        holder.viewItemBinding.momentGridViewItemImage.setOnClickListener(v -> {
            // 启动图片预览活动
            Intent intent = new Intent(activity, ImagePreviewActivity.class);
            intent.putExtra("index", position);
            intent.putExtra("imageList", GsonInstance.instance.toJson(imageUriList));
            Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, holder.viewItemBinding.getRoot(), "image_transition").toBundle();
            activity.startActivity(intent, bundle);
        });
        Glide.with(activity).load(OkHttp.URL+imageUriList.get(position)).into(holder.viewItemBinding.momentGridViewItemImage);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return imageUriList.size();
    }
}
